package com.gray.wow.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gray.wow.biz.TagBiz;
import com.gray.wow.dao.impl.ContentDaoImpl;
import com.gray.wow.domain.PageModel;
import com.gray.wow.domain.Tag;

@Service("tagBiz")
@Transactional
public class TagBizImpl implements TagBiz{

	@Resource
	private ContentDaoImpl contentDao;
	
	@Override
	public PageModel getTag(Integer pageSize,Integer page) {
		Map<String, Object> map=new HashMap<String, Object>();
		int count = 0;
		if(pageSize!=null){
			count=contentDao.getTagCount(map);
			page = page == null ? 1 : page;
			map.put("pageSize", pageSize);
			map.put("page", page);
		}
		
		List<Tag> list=contentDao.getTag(map);
		PageModel pm = PageModel.getPageModel(pageSize, page, count, list);
		return pm;
	}

	@Override
	public boolean saveTag(Tag tag) {
		if(tag!=null){
			if(tag.getId()!=null){
				if(contentDao.updateTag(tag)==1)return true;
			}else{
				if(contentDao.insertTag(tag)==1)return true;
			}
		}
		return false;
	}

	@Override
	public boolean delTag(int... ids) {
		if(ids!=null&&ids.length>0){
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("ids", ids);
			if(contentDao.deleteTag(map)==ids.length)return true;
		}
		return false;
		
	}

	@Override
	public Tag getTagById(Integer id) {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("id", id);
		List<Tag> list=contentDao.getTag(map);
		if(list!=null && list.size()==1){
			return list.get(0);
		}
		return null;
	}

}
